package com.wang.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wang.dao.IHandler;
import com.wang.domain.Student;
import com.wang.util.DBCPUtil;

public class BaseDaoImpl{
	public static<T> List<T> sqlQuery(String sql,IHandler<T> handler,Object... obj) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		List<T> arrayList = new ArrayList<T>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				prepareStatement.setObject(i+1, obj[i]);
			}
			executeQuery = prepareStatement.executeQuery();
			arrayList = handler.handler(executeQuery);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return arrayList;
	}
	public static Integer sqlExecute(String sql,Object... obj) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		int executeUpdate = -1;
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement(sql);
			for (int i = 0; i < obj.length; i++) {
				prepareStatement.setObject(i+1, obj[i]);
			}
			executeUpdate = prepareStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return executeUpdate;
	}
}
